package com.li.controller.listener;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @program: srpingboot-rocketmq
 * @description: 消息事务消费者
 * @author: li
 * @create: 2022-09-26 15:36
 **/
@Component
@RocketMQMessageListener(topic = "TOPIC_TEST_TRANS",
        consumerGroup = "trans_product_group_name")
public class TransMessageConsumer implements RocketMQListener<MessageExt>, RocketMQPushConsumerLifecycleListener {
    private static final Logger logger = LoggerFactory.getLogger(TransMessageConsumer.class);


    @Override
    public void onMessage(MessageExt message) {
        logger.info("TransMessageConsumer consumer 收到消息：{}", new String(message.getBody()));
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
        consumer.setMaxReconsumeTimes(20);
    }
}
